﻿using System;
using System.Globalization;
using System.Windows.Data;
using OpenFileTransfer.Wpf.Models;

namespace OpenFileTransfer.Wpf.Tools.ValueConverters
{
    [ValueConversion(typeof(MenuType), typeof(bool?))]
    public class MenuType2IsCheckedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(bool?))
            {
                throw new InvalidOperationException("The target must be a bool");
            }

            if (!Enum.IsDefined(typeof(MenuType), value))
            {
                throw new InvalidOperationException("The value must be a MenuType");
            }

            if (!Enum.IsDefined(typeof(MenuType), parameter))
            {
                throw new InvalidOperationException("The parameter must be a MenuType");
            }

            if (value == null)
            {
                throw new ArgumentNullException();
            }

            return (MenuType)value == (MenuType)parameter;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(MenuType))
            {
                throw new InvalidOperationException("The target must be a MenuType");
            }

            if (!Enum.IsDefined(typeof(MenuType), parameter))
            {
                throw new InvalidOperationException("The parameter must be a MenuType");
            }

            if (value is not bool isChecked)
            {
                throw new InvalidOperationException("The value must be a bool");
            }

            return isChecked ? (MenuType)parameter : Binding.DoNothing;
        }
    }
}
